#include "serialportthread.h"


SerialPortThread::SerialPortThread(QDialog *parent)
{
    pGridLayout = new QGridLayout;
    pComBoxPort = new QComboBox;
    pComBoxBaud = new QComboBox;
    pBtn_connect = new QPushButton;
    pBtn_connect->setText(tr("connect"));
    pGridLayout->addWidget(pComBoxPort);
    pGridLayout->addWidget(pComBoxBaud);
    pGridLayout->addWidget(pBtn_connect);
    setLayout(pGridLayout);

//     pComBoxPort->clear();
//     pComBoxBaud->clear();
//     searialPortInfos.clear();
//     _SerialPortInfo searialInfos;
//     const auto infos = QSerialPortInfo::availablePorts();
//     for (const QSerialPortInfo &info : infos) {
//         searialInfos.portName = info.portName();
//         searialInfos.systemLocation = info.systemLocation();
//         searialInfos.description = info.description();
//         searialInfos.manufacturer = info.manufacturer();
//         searialInfos.serialNumber = info.serialNumber();
// #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
// #else
//         searialInfos.hasVendorIdentifier = info.hasVendorIdentifier();
//         searialInfos.hasProductIdentifier = info.hasProductIdentifier();
// #endif
//         pComBoxPort->addItem(info.portName() + "(" + info.description() + ")" );
//         searialPortInfos.push_back(searialInfos);
//     }
//     pComBoxBaud->addItem(QString::number(QSerialPort::Baud38400));
//     pComBoxBaud->addItem(QString::number(QSerialPort::Baud57600));
//     pComBoxBaud->addItem(QString::number(QSerialPort::Baud115200));
    // pComBoxBaud->setCurrentIndex(2);
    update_serial_port();

    connect(pBtn_connect,&QPushButton::clicked,this,&SerialPortThread::on_btn_connect);
    serialIsOpen = false;

}

bool SerialPortThread::open_port(const QString portName, const int baud)
{
    serialPortName = portName;
    serialPortBaud = baud;
    connect(&serial,&QSerialPort::readyRead,this,&SerialPortThread::serialReadyRead);
    //if(!isRunning())
    if(serialIsOpen == false)
    {
        serial.setPortName(serialPortName);
        serial.setBaudRate(serialPortBaud);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        if (!serial.open(QIODevice::ReadWrite)) {
            emit error(tr("connot open serial port:%s").arg(serialPortName));
            return false;
        }
        // serialIsOpen = true;
        //start();
        return true;
    }
//    else
//    {
//        serial.close();
//        serialIsOpen = false;
//    }
    return false;
}

void SerialPortThread::update_serial_port()
{
    pComBoxPort->clear();
    pComBoxBaud->clear();
    searialPortInfos.clear();
    _SerialPortInfo searialInfos;
    const auto infos = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &info : infos) {
        searialInfos.portName = info.portName();
        searialInfos.systemLocation = info.systemLocation();
        searialInfos.description = info.description();
        searialInfos.manufacturer = info.manufacturer();
        searialInfos.serialNumber = info.serialNumber();
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#else
        searialInfos.hasVendorIdentifier = info.hasVendorIdentifier();
        searialInfos.hasProductIdentifier = info.hasProductIdentifier();
#endif
        pComBoxPort->addItem(info.portName() + "(" + info.description() + ")" );
        searialPortInfos.push_back(searialInfos);
    }
    // pComBoxBaud->addItem(QString::number(QSerialPort::Baud38400));
    // pComBoxBaud->addItem(QString::number(QSerialPort::Baud57600));
    pComBoxBaud->addItem(QString::number(QSerialPort::Baud115200));
}

void SerialPortThread::disconnect()
{
    serial.clear();
    serial.close();
    serialIsOpen = false;
}

// void SerialPortThread::run()
// {
// //    serial.setPortName(serialPortName);
// //    serial.setBaudRate(serialPortBaud);
// //    serial.setDataBits(QSerialPort::Data8);
// //    serial.setParity(QSerialPort::NoParity);
// //    serial.setStopBits(QSerialPort::OneStop);
// //    serial.setFlowControl(QSerialPort::NoFlowControl);
// //    if (!serial.open(QIODevice::ReadWrite)) {
// //        emit error(tr("connot open serial port:%s").arg(serialPortName));
// //        return;
// //    }
// //    while (!quit)
// //    {

// //    }
// //    serial.close();
// }

void SerialPortThread::serialReadyRead()
{
    searial_read_data.clear();
    searial_read_data = serial.readAll();
    emit receiveData(searial_read_data);
}

void SerialPortThread::on_btn_connect()
{
    if(serialIsOpen)
    {
        disconnect();
        pBtn_connect->setText(tr("connect"));
        serialIsOpen = false;
        pComBoxBaud->setEnabled(true);
        pComBoxPort->setEnabled(true);
        emit sig_communication_disconnect();
    }
    else
    {
        if(open_port(searialPortInfos[pComBoxPort->currentIndex()].portName,115200) == true)
        {
            pBtn_connect->setText(tr("disconnect"));
            serialIsOpen = true;
            pComBoxBaud->setEnabled(false);
            pComBoxPort->setEnabled(false);
            emit sig_communication_connection(true);
        }
        else
        {
            emit sig_communication_connection(false);
        }
    }
}

void SerialPortThread::on_send_data(QByteArray data)
{
    serialWriteData(data);
}

void SerialPortThread::serialWriteData(QByteArray &byteArray)
{
    qint64 len = byteArray.length();
    qint64 count = 0 ;
    if(serialIsOpen == false)
    {
        return ;
    }
    while(count < len)
    {
        qint64 i = serial.write(byteArray.data() + count,len - count);
        if(i > 0)
        {
            count += i;
        }
        else
        {
            emit error(tr("serial write error."));
            break;
        }
    }
}
